package com.jinlejia.erp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.ItemMultiSheetBinding;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 底部多选选择Adapter
 */
public class MultiSheetAdapter extends YfListAdapter<Map<String, Object>> {

    private Context context;
    private ArrayList<Map<String, Object>> selectList = new ArrayList<>();
    private List<String> list ;

    public MultiSheetAdapter(ArrayList<Map<String, Object>> data,List<String> list) {
        super(data);
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        context = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_multi_sheet, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);

        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder viewHolder, final int i) {
        final ItemMultiSheetBinding binding = DataBindingUtil.getBinding(viewHolder.itemView);
        final Map<String, Object> map = mData.get(i);
        if (mData.get(i).containsKey("valueName")) {
            binding.text.setText(map.get("valueName").toString());
            if( list.contains( map.get("valuel") ) ){
                binding.checkbox.setChecked(true);
                selectList.add(map);
            }
        }
        if (mData.get(i).containsKey("name")) {
            binding.text.setText(map.get("name").toString());
            if( list.contains( map.get("value") ) ){
                binding.checkbox.setChecked(true);
                selectList.add(map);
            }
        }

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectList.contains(map)) {
                    binding.checkbox.setChecked(false);
                    selectList.remove(map);
                } else {
                    binding.checkbox.setChecked(true);
                    selectList.add(map);
                }
            }
        });

        binding.getRoot().setTag(mData.get(i));
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_loading, parent, false);
        return new YfSimpleViewHolder(view);
    }


    static final class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }

    public String getNameString() {
        StringBuffer sb = new StringBuffer();

        for (Map<String, Object> map : selectList) {
            if (map.containsKey("valueName"))
                sb = sb.append(";").append(map.get("valueName"));
            if (map.containsKey("name"))
                sb = sb.append(";").append(map.get("name"));
        }

        return sb.toString().replaceFirst(";", "");
    }

    public String getValueString() {
        StringBuffer sb = new StringBuffer();

        for (Map<String, Object> map : selectList) {
            if (map.containsKey("valuel"))
                sb = sb.append(";").append(map.get("valuel"));
            if (map.containsKey("value"))
                sb = sb.append(";").append(map.get("value"));
        }

        return sb.toString().replaceFirst(";", "");
    }

}
